<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/9/19
 * Time: 15:16
 */
$arr = range(1,5);
//$a = array_reduce($arr,function($r,$v){
//    var_dump(func_get_args());
//    return $r+$v;
//});
//var_dump($a);


function my_reduce($arr,$callback){
    $r = null;
    foreach($arr as $k=>$v){
        $r = $callback($r,$v);
    }
    return $r;
}
$a = my_reduce($arr,function($r,$v){
    return $r+$v;
});
var_dump($a);
$a = my_reduce($arr,function($r,$v){

    return $r."-".$v;
});
var_dump($a);
$a = my_reduce($arr,function($r,$v){
    if (is_null($r)){
        $r = 1;
    }
    return $r*$v;
});
var_dump($a);
